<template>
    <div class="app-container">
      <!-- 查询表单 -->
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="服务选项名称">
          <el-input v-model="queryParams.serviceOptionName" placeholder="请输入服务选项名称" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <!-- 操作按钮 -->
      <el-row :gutter="10" class="mb8">
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <!-- 订单列表 -->
      <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="编号" align="center" type="index" />
        <el-table-column label="订单号" align="center" prop="orderNumber" />
        <el-table-column label="服务选项名称" align="center" prop="serviceOptionName" />
        <el-table-column label="预约时间" align="center" prop="appointmentTime" />
        <el-table-column label="服务地址" align="center" prop="serviceAddress" />
        <el-table-column label="订单状态" align="center" prop="orderStatus">
          <template slot-scope="scope">
            {{ getOrderStatusText(scope.row.orderStatus) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(scope.row)" v-hasPermi="['order:detail']">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
  
      <!-- 分页 -->
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
  
      <!-- 订单详情对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
        <el-form label-width="100px">
          <el-form-item label="服务选项名称">
            {{ form.serviceOptionName }}
          </el-form-item>
          <el-form-item label="用户昵称">
            {{ form.userNickName }}
          </el-form-item>
          <el-form-item label="工程师名称">
            {{ form.engineerName }}
          </el-form-item>
          <el-form-item label="预约时间">
            {{ form.appointmentTime }}
          </el-form-item>
          <el-form-item label="服务地址">
            {{ form.serviceAddress }}
          </el-form-item>
          <el-form-item label="联系人">
            {{ form.contactName}}
          </el-form-item>
          <el-form-item label="联系人电话">
            {{ form.contactPhone}}
          </el-form-item>
          <el-form-item label="订单状态">
            {{ getOrderStatusText(form.orderStatus) }}
          </el-form-item>
          <el-form-item label="取消原因">
            {{ form.cancelReason }}
          </el-form-item>
          <el-form-item label="上门费用">
            {{ form.serviceFee }}
          </el-form-item>
          <el-form-item label="维修费用">
            {{ form.repairFee }}
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel">关 闭</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import { listOrders, getOrder } from "@/api/order/order"
  
  export default {
    name: "Order",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 订单列表
        orderList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          serviceOptionName: "",
        },
        // 表单参数
        form: {},
      };
    },
    created() {
      this.getList();
    },
    methods: {
  
      /** 查询订单列表 */
      getList() {
        this.loading = true;
        listOrders(this.queryParams).then(response => {
          this.orderList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          userId: null,
          serviceOptionName: null,
          appointmentTime: null,
          serviceAddress: null,
          orderStatus: null,
          serviceFee: null,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id);
        this.single = selection.length !== 1;
        this.multiple = !selection.length;
      },
      /** 详情按钮操作 */
      handleDetail(row) {
        this.reset();
        const id = row.id;
        getOrder(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "订单详情";
        });
      },
  
      /** 获取订单状态文本 */
      getOrderStatusText(status) {
        switch (status) {
          case 0:
            return '待抢单';
          case 1:
            return '待上门';
          case 2:
            return '待支付';
          case 3:
            return '已支付';
          case 4:
            return '已完成';
          case 5:
            return '已取消';
          case 6:
            return '已评价';
          case 7:
            return '已售后';
          default:
            return '未知状态';
        }
      }
    }
  };
  </script>
  
  <style scoped>
  .app-container {
    padding: 20px;
  }
  
  .el-form-item {
    margin-bottom: 15px;
  }
  
  .el-table {
    margin-top: 20px;
  }
  </style>